package com.xie.smfs.effect;

import com.xie.smfs.entity.QiaomenGhostEntity;
import com.xie.smfs.registry.ModEffects;
import com.xie.smfs.registry.ModEntities;
import com.xie.smfs.registry.ModSounds;
import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Random;

/**
 * 敲门诅咒效果 - 6分钟后转化为鬼敲门效果并生成敲门鬼
 */
public class KnockingCurseEffect extends StatusEffect implements ICurseEffect {
    private static final int CURSE_DURATION = 7200; // 6分钟 = 6*60*20
    private static final int GHOST_KNOCK_DURATION = 36000; // 30分钟 = 30*60*20
    private static final Random random = new Random();

    public KnockingCurseEffect() {
        super(StatusEffectCategory.HARMFUL, 0x4a2c2a); // 暗红色
    }

    @Override
    public boolean canApplyUpdateEffect(int duration, int amplifier) {
        // 改为每20tick(1秒)更新一次以显示倒计时
        return duration % 20 == 0 || duration == 1;
    }

    @Override
    public void applyUpdateEffect(LivingEntity entity, int amplifier) {
        if (entity instanceof PlayerEntity player && !player.getWorld().isClient) {
            StatusEffectInstance effect = player.getStatusEffect(ModEffects.KNOCKING_CURSE);
            if (effect == null) return;

            int duration = effect.getDuration();

            // 移除环境检查和声音播放逻辑，仅保留倒计时功能

            // 最后一刻触发效果转化
            if (duration == 1) {
                player.removeStatusEffect(ModEffects.KNOCKING_CURSE);
                player.addStatusEffect(new StatusEffectInstance(ModEffects.GHOST_KNOCK, GHOST_KNOCK_DURATION, 0));
                spawnQiaomenGhost(player.getWorld(), player.getBlockPos());
            }
        }
    }

    // 生成敲门鬼的私有静态方法
    private static void spawnQiaomenGhost(World world, BlockPos pos) {
        if (!world.isClient ) {
            QiaomenGhostEntity ghost = new QiaomenGhostEntity(ModEntities.QIAOMEN_GHOST, world);
            ghost.refreshPositionAndAngles(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, 0.0F, 0.0F);
            ghost.setPersistent();
            world.spawnEntity(ghost);
            // 播放出现音效
            world.playSound(null, pos, ModSounds.GHOST_APPEAR, SoundCategory.HOSTILE, 1.0F, 1.0F);
        }
    } // 这里添加了缺失的大括号

    @Override
    public String getTranslationKey() {
        return "effect.smfs.knocking_curse";
    }
}